package com.yn.courseselectionsystem.interceptor;

import com.yn.courseselectionsystem.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @description: TODO
 * @author: Tokyo_God
 * @date: 2021/1/2 10:25
 * Role of:拦截为登入的用户
 * @version: v1.0
 */
@Component
public class InterceptorUserNull implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession();
        System.out.println("开始拦截");
        User user= (User) session.getAttribute("user");
        if(user==null){
            System.out.println("监测到没有登入 拦截！");
            response.sendRedirect("/");
            return false;
        }else{
            System.out.println("监测到已登入 放行！");
            return true;
        }
    }
}
